package top.frsf.rpc.protocol;

import lombok.Data;
import top.frsf.rpc.constants.RpcConstants;
import top.frsf.rpc.protocol.head.RpcHeader;

/**
 * RPC消息
 *
 * @author He Kuntao
 * @since 2025/7/2 15:24
 */
@Data
public class RpcMessage<T> {

    private RpcHeader header;

    private T body;

    /**
     * 构建请求消息
     *
     * @param body 消息体
     * @return RpcMessage
     */
    public static <T> RpcMessage<T> builderReqMsg(T body) {
        // 请求头
        RpcHeader rpcHeader = RpcHeader.builder()
                .magic(RpcConstants.MAGIC)
                .version(RpcConstants.VERSION)
                .flags(RpcConstants.FLAG_REQUEST)
                .serialType(RpcConstants.SERIALIZER_JSON)
                .status(RpcConstants.STATUS_OK)
                .requestId(System.currentTimeMillis())
                .build();
        RpcMessage<T> message = new RpcMessage<>();
        message.setHeader(rpcHeader);
        message.setBody(body);
        return message;
    }
}
